package com.lucker.service.recommendplan;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.lucker.cds.model.CRecommendPlan;
import com.lucker.ds.mapper.BaseCustomMapper;
import com.lucker.ds.model.RecommendPlan;
import com.lucker.framework.exception.BaseServiceException;
import com.lucker.service.BaseServiceImpl;

@Service
public class RecommendPlanServiceImpl extends BaseServiceImpl<RecommendPlan, CRecommendPlan, Long>
		implements RecommendPlanService {

	@Override
	public BaseCustomMapper<CRecommendPlan, Long> getCustomMapper() {
		return null;
	}

	public List<RecommendPlan> getRecommendPlanListByParentId(String recommendId) {
		RecommendPlan query = new RecommendPlan();
		query.setParentPlan(recommendId);
		return queryAll(query);
	}

	public List<Map<String, Object>> getAllPlay(String caiType) {
		if(StringUtils.isBlank(caiType)) {
			throw new BaseServiceException("缺少caiType参数");
		}
		
		RecommendPlan _plan = new RecommendPlan();
		_plan.setCaiTypeId(caiType);
		List<RecommendPlan> list = queryAll(_plan);
		
		List<Map<String, Object>> nodeList = new ArrayList<Map<String, Object>>();
		for(RecommendPlan p : list) {
			Map<String,Object> map = new HashMap<String,Object>();
			//通过判断，0代表没有父级，也就是一级
			   if(StringUtils.equals("-1", p.getParentPlan())){
			      map.put("childNodes", getChildren(list,p.getPlanId()));//继续往一级下面遍历，调用下面的处理方法，放入树状结构
			      map.put("id", p.getId());
			      map.put("name", p.getPlanName());
			      map.put("times", 12);
			      nodeList.add(map);//先把一级存入结果
			   }
		}
		
		//getLOGGER().error("遍历后父节点数据 ：    " + JSONObject.toJSONString(MenuList));
		return nodeList;

	}
	
	public List<Map<String, Object>> getChildren(List<RecommendPlan> data ,String planId){//参数为数据库的（原数据，一级id）
		   List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
		   if(CollectionUtils.isEmpty(data)) {
			   return list;
		   }
		   for(RecommendPlan plan : data ){
		      Map<String,Object> map = new HashMap<String,Object>();
		      if(StringUtils.equals(planId, plan.getParentPlan())){//如果本级id与下面数据的父id相同，就说明是子父级关系
		         map.put("childNodes", getChildren(data,plan.getPlanId()));//递归，查询子级下的子级
		         map.put("id", plan.getId());
		         map.put("name", plan.getPlanName());
		         map.put("times", 12);
		         list.add(map);
		      }
		   }
		   return list;
		}
	
	

}
